package com.bawei.yangzongcai___chenmei.SlindingMenu;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bawei.yangzongcai___chenmei.JavaClass.JavaFeiZhou;
import com.bawei.yangzongcai___chenmei.R;
import com.bawei.yangzongcai___chenmei.SQL.SQLyongyong3;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class SlingMenuActivity5 extends AppCompatActivity {
    private ListView listveiw;
    List<JavaFeiZhou.DataBean> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sling_menu5);
        initView();

        SharedPreferences sp = getSharedPreferences("is", MODE_PRIVATE);
        boolean is = sp.getBoolean("is", false);
        if (is){
            //从数据库里获取数据
            //1.db  2.增删改查

           SQLyongyong3 foodSql = new SQLyongyong3(SlingMenuActivity5.this, "chen.dp", null, 1);
            SQLiteDatabase db = foodSql.getReadableDatabase();
            Cursor cursor = db.query("ertudi", null, null, null, null, null, null);
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndex("diyi"));
                String title = cursor.getString(cursor.getColumnIndex("dier"));
                String pic = cursor.getString(cursor.getColumnIndex("disan"));

                JavaFeiZhou.DataBean dataBeansql = new JavaFeiZhou.DataBean();
                dataBeansql.setTitle(id);
                dataBeansql.setPic(title);
                dataBeansql.setPic(pic);
                //添加到集合
             list.add(dataBeansql);//数据源
            }

            //适配器
            Myadpter foodAdapter = new Myadpter();
            //设置适配器
            listveiw.setAdapter(foodAdapter);

            ToastUtils.showShort("数据库");

        }
        else
        {
            ToastUtils.showShort("网络");
            new Myabcopent().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=40&page=4");

        }


    }

    private void initView() {
        listveiw = findViewById(R.id.listveiw);
    }


    public class  Myabcopent extends AsyncTask<String,Void,String>
    {

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.connect();
                if(httpURLConnection.getResponseCode()==200)
                {
                    InputStream inputStream=httpURLConnection.getInputStream();
                    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                    String str="";
                    StringBuffer stringBuffer=new StringBuffer();
                    while ((str=bufferedReader.readLine())!=null)
                    {
                        stringBuffer.append(str);
                    }
                    String json=stringBuffer.toString();
                    return json;

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        //---------------

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s!=null)
            {
                Gson gson= new Gson();
                JavaFeiZhou feiZhou =gson.fromJson(s,JavaFeiZhou.class);
                list = feiZhou.getData();
                //往sp里写一个值 表示数据已经存进数据库了
                SharedPreferences sp = getSharedPreferences("is", MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putBoolean("is", true);
                edit.commit();

                SQLyongyong3 sqLyongyong3 = new SQLyongyong3(SlingMenuActivity5.this,"chen.dp",null,1);
                SQLiteDatabase db = sqLyongyong3.getReadableDatabase();
                for (JavaFeiZhou.DataBean dataBean : list) {
                    db.execSQL("insert into ertudi values (?,?,?)",new Object[]{dataBean.getTitle(),dataBean.getPic(),dataBean.getPic()});
                }




                Myadpter myadpter = new Myadpter();
                listveiw.setAdapter(myadpter);
            }
        }
    }

    public class Myadpter extends BaseAdapter
    {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Verhoder viewHolder = new Verhoder();
            if(convertView==null)
            {
                convertView= LayoutInflater.from(SlingMenuActivity5.this).inflate(R.layout.wqwadpteractivity1,null);
                viewHolder.textOpen=convertView.findViewById(R.id.wqw1_text);
                viewHolder.imagOpen1=convertView.findViewById(R.id.wqw1_img1);
                viewHolder.imageOpen2=convertView.findViewById(R.id.wqw1_img2);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (Verhoder) convertView.getTag();
            }

            viewHolder.textOpen.setText(list.get(position).getTitle().toString());
            Glide.with(SlingMenuActivity5.this).load(list.get(position).getPic()).into(viewHolder.imagOpen1);
            Glide.with(SlingMenuActivity5.this).load(list.get(position).getPic()).into(viewHolder.imageOpen2);
            return convertView;
        }
    }

    public class Verhoder
    {
        TextView textOpen;
        ImageView imagOpen1;
        ImageView imageOpen2;
    }




}
